﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Nancy.Hosting.Self;

namespace YAPSAR
{
    class WebApplication
    {
        private static WebApplication _webApplication;
        public static WebApplication Instance
        {
            get
            {
                if (_webApplication == null)
                {
                    _webApplication = new WebApplication();
                }

                return _webApplication;
            }
        }

        private NancyHost host;
        private Thread thread;

        private WebApplication()
        {
        }

        public void Start()
        {
            HostConfiguration configuration = new HostConfiguration();
            configuration.UrlReservations.CreateAutomatically = true;

            this.host = new NancyHost(configuration, new Uri("http://localhost:8731"));

            this.thread = new Thread(new ThreadStart(() =>
            {
                host.Start();
            }));

            thread.IsBackground = true;
            thread.Priority = ThreadPriority.BelowNormal;
            thread.Start();
        }

        public void Stop()
        {
            host.Stop();
            host.Dispose();

            this.thread.Abort();
        }
    }
}
